package com.calm.cdemo.anim;

import android.animation.Animator;
import android.animation.ObjectAnimator;
import android.view.View;
import android.widget.TextView;

import com.calm.cdemo.R;
import com.calm.cdemo.base.BaseActivity;

/**
 * Create by Calm
 * 2020/3/5 17:01
 */
public class ObjectAnim extends BaseActivity implements View.OnClickListener {
    private TextView tvLabel;
    private ObjectAnimator animator;
    @Override
    protected void init() {
        tvLabel = findViewById(R.id.objectanim_tv_label);
        findViewById(R.id.objectanim_btn_start).setOnClickListener(this);
        findViewById(R.id.objectanim_btn_stop).setOnClickListener(this);
    }

    @Override
    protected int getLayoutId() {
        return R.layout.objectanim;
    }

    @Override
    public void onClick(View view) {
        switch (view.getId()){
            case R.id.objectanim_btn_start:
                doAplhaAnim();
                break;
            case R.id.objectanim_btn_stop:
                stopAplhaAnim();
                break;
        }
    }
    private void doAplhaAnim(){
        animator = ObjectAnimator.ofFloat(tvLabel,"alpha",1,0);
        animator.setDuration(1000);
        animator.setRepeatCount(ObjectAnimator.INFINITE);
        animator.setRepeatMode(ObjectAnimator.RESTART);
        animator.start();
    }
    private void stopAplhaAnim(){
        if(animator != null && animator.isRunning()){
            animator.end();
            animator = null;
        }
    }
}
